package com.serialcommunication.gui.component;

import static com.serialcommunication.constants.GuiConfiguration.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.util.Arrays;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;

import org.apache.soap.encoding.Hex;

import com.serialcommunication.controller.Coordinator;
import com.serialcommunication.enums.Engine;
import com.serialcommunication.enums.Sign;
import com.serialcommunication.gui.frame.MainFrame;
import com.serialcommunication.helper.HexUtil;
import com.serialcommunication.interfaces.IAction;
import com.serialcommunication.interfaces.IBaseComponent;
import com.serialcommunication.interfaces.ITransmitComponent;
import com.serialcommunication.listener.MyChangeListener;
import com.serialcommunication.listener.MyMouseListener;

public class TextFieldFixedValuesComponent extends BaseComponent implements ITransmitComponent {
	
	private int pointer = 0;
	private List<String> values;
	
	private JLabel label;
	private JButton increaseButton;
	private JButton decreaseButton;
	private JTextField textField;

	public TextFieldFixedValuesComponent(Engine pEngine, IAction pAction, MainFrame pFrame, List<String> pValues) {
		super(pEngine, pAction, pFrame);
		
		getPanel().setLayout(null);
		getPanel().setBackground(new Color(234, 237, 241));
		getPanel().setBorder(BorderFactory.createLineBorder(new Color(179, 181, 184)));
		
		values = pValues;
		label = new JLabel(pAction.getTitle());
		textField = new JTextField(values.get(0));
		textField.setEditable(false);
		increaseButton = new JButton("+");
		increaseButton.setBorder(null);
		decreaseButton = new JButton("-");
		decreaseButton.setBorder(null);
		initListener();
		setSizeOfElementsAndPanel();
		addComponentsToPanel();
		alignElementsWithinPanel();
		label.setFont(new Font(Font.DIALOG, Font.PLAIN, 11));
		textField.setFont(new Font(Font.DIALOG, Font.PLAIN, 11));
		disableComponent();
	}
	
	public void initListener() {
		decreaseButton.addMouseListener(new MyMouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				decreaseValue();
			}
		});
		increaseButton.addMouseListener(new MyMouseListener() {
			@Override
			public void mouseReleased(MouseEvent e) {
				increaseValue();
			}
		});
	}
	
	private void increaseValue() {
		if (pointer + 1  < values.size()) {
			textField.setText(values.get(++pointer));
		} else {
			textField.setText(values.get(0));
			pointer = 0;
		}
	}
	
	private void decreaseValue() {
		if (pointer > 0) {
			textField.setText(values.get(--pointer));
		} else {
			textField.setText(values.get(values.size()-1));
			pointer = values.size() - 1;
		}
	}
	
	public void addComponentsToPanel() {
		getPanel().add(label);
		getPanel().add(increaseButton);
		getPanel().add(decreaseButton);
		getPanel().add(textField);
	}
	
	public void setSizeOfElementsAndPanel(){
		getPanel().setSize(PANEL_FIXVALUE_SIZE_2);
		label.setSize(LABEL_SIZE);
		increaseButton.setSize(BUTTON_SIGN_SIZE);
		decreaseButton.setSize(BUTTON_SIGN_SIZE);
		textField.setSize(TEXTFIELD_SIZE_4);
	}
	
	// posibility 1
//	public void alignElementsWithinPanel(){
//		label.setLocation(PANEL_ELEMENT_PADDING_X, PANEL_ELEMENT_PADDING_Y);
//		textField.setLocation(PANEL_ELEMENT_PADDING_X,  PANEL_ELEMENT_PADDING_Y + label.getHeight());
//		decreaseButton.setLocation(PANEL_ELEMENT_PADDING_X + textField.getWidth(),  PANEL_ELEMENT_PADDING_Y+ label.getHeight());
//		increaseButton.setLocation(PANEL_ELEMENT_PADDING_X + textField.getWidth() + decreaseButton.getWidth(), PANEL_ELEMENT_PADDING_Y + label.getHeight());
//	}
	
	public void alignElementsWithinPanel(){
		label.setLocation(PANEL_ELEMENT_PADDING_X, PANEL_ELEMENT_PADDING_Y);
		textField.setLocation(PANEL_ELEMENT_PADDING_X ,  PANEL_ELEMENT_PADDING_Y + label.getHeight());
		decreaseButton.setLocation(PANEL_ELEMENT_PADDING_X + textField.getWidth(),  PANEL_ELEMENT_PADDING_Y + label.getHeight());
		increaseButton.setLocation(PANEL_ELEMENT_PADDING_X + textField.getWidth() + decreaseButton.getWidth() , PANEL_ELEMENT_PADDING_Y + label.getHeight()) ;
	}
	
	
	@Override
	public void setValue(String pValue) {
		textField.setText(pValue);
	}

	@Override
	public String getValue() {
		return textField.getText();
	}

	@Override
	public void transmitData() {
		// String is in dez. Here a convert to hex value is needed  TO DO
	}

	@Override
	public void disableComponent() {
		increaseButton.setEnabled(false);
		decreaseButton.setEnabled(false);
		textField.setEnabled(false);
		label.setEnabled(false);
	}

	@Override
	public void enableComponent() {
		increaseButton.setEnabled(true);
		decreaseButton.setEnabled(true);
		textField.setEnabled(true);
		label.setEnabled(true);
	}

	@Override
	public void setInActive() {
		// not needed
	}

	@Override
	public void setActive() {
		// not needed
	}

}
